import datetime
from ExcelProcess import read_online_excel, gen_project_dict, project_classification, \
    post_action, post_content,post_project_content
from KdocsDownload import kdocs_download
import schedule
import time

def delay_project(sheet_project):
    """项目阶段延迟字段组装"""
    # global count
    count = 1
    ncols=sheet_project.ncols
    # global result
    result = r"### 以下项目延后，请相关同事注意 \n "
    for i in range(1,ncols-1):
        if(sheet_project.cell_value(2,i)=="延后"):
            result += ">"+str(count)+"."+sheet_project.cell_value(0,i)+r" \n "
            result += r'<font color=\"warning\">' + sheet_project.cell_value(3,i) + r'</font>'+ "->" + r'<font color=\"info\">'+ sheet_project.cell_value(4,i)+ r'</font>' +r" \n "
            count += 1
    result=result+"@ all"
    return result,count


def job():
    # url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4c0b1f2d-5d6f-4807-9d40-1da4e46b0915'
    # url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=69280f46-4747-4774-a35b-a537eebc9818'
    current_day = datetime.datetime.today()
    today = datetime.datetime(current_day.year, current_day.month, current_day.day)

    sht_all, sheet2 = read_online_excel(kdocs_download())

    upcoming_projects, endcoming_projects = project_classification(sht_all, today)
    upcoming_count = len(upcoming_projects)
    endcoming_count = len(endcoming_projects)

    # print('即将到期任务个数：',endcoming_count)
    # print('即将开始任务个数', upcoming_count)
    # print('==========================================')

    final_content = post_content(post_project_content(upcoming_projects), post_project_content(endcoming_projects))
    # print(final_content)
    coming_result = post_action(url, final_content)
    # print('coming_result发送状态：',coming_result)
    # coming_result = 'success'
    # print('==========================================')


    delay_content, delay_count = delay_project(sheet2)
    # print(result)
    # print('阶段延迟任务个数：',delay_count)
    delay_result = post_action(url, delay_content)
    # print('delay_result发送状态：',delay_result)
    # delay_result = 'success'
    # print('==========================================')

    # 生成统计推送
    num_statistic = '### 以上为%s项目提醒事项 ' % today.strftime(
        '%Y-%m-%d') + r'\n' + '阶段即将开始 %d 条' % upcoming_count + r'\n' + '阶段即将到期 %d 条' % endcoming_count + r'\n' + \
                    '阶段延迟 %d 条' % (delay_count-1) + r'\n 请相关同事重点关注\n@所有人'
    # print(num_statistic)
    statis_result = post_action(url, num_statistic)
    # print('statis_result发送状态：', statis_result)
    # statis_result = 'success'

    with open('test.txt', 'a') as f:
        f.write('====================%s===================='%today + '\r\n')
        f.write(num_statistic+'\r\n')
        f.write('coming_result发送状态：'+coming_result+'\r\n'+'delay_result发送状态：'+delay_result+'\r\n'+'statis_result发送状态：'+statis_result+'\r\n')

if __name__ == '__main__':
    # schedule.every().day.at('11:30').do(job)
    # while True:
    #     schedule.run_pending()
    #     time.sleep(30)
    job()
